//
//  ClassifyDetailViewController.m
//  Happy Kitchen
//
//  Created by 冫柒Yun on 15/3/9.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "ClassifyDetailViewController.h"

@interface ClassifyDetailViewController ()

@end

@implementation ClassifyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 根据点击上一个面的cell设置导航栏的标题
    self.navigationItem.title = self.titleName;
    
    // 瀑布流类
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 2 - 10, self.view.frame.size.height / 3);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 7, 10, 7);
    
    // 集合视图
    self.detailCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 64) collectionViewLayout:flowLayout];
    self.detailCollectionView.backgroundColor = [UIColor whiteColor];
    
    // 指定代理人
    self.detailCollectionView.dataSource = self;
    self.detailCollectionView.delegate = self;
    
    [self.view addSubview:self.detailCollectionView];
    
    
    // 注册cell
    [self.detailCollectionView registerClass:[ClassifyDetailCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    // 初始化数组
    self.allClassifyListDetaiArr = [NSMutableArray array];
    
    self.isUpLoading = NO;
    
    // 集成上拉下拉功能
    [self addHeader];
    [self addFooter];
    
    
    // 菊花类
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    
    self.HUD.labelText = @"加载中";
    
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        
        [self doTask];
    } completionBlock:^{
        
        
    }];
    
}
// 菊花类自定义方法(最长显示时间20秒)
- (void)doTask
{
    sleep(20);
}

// item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allClassifyListDetaiArr.count;
}
// 设置item内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    ClassifyListDetaiModel *classifyListDetai = [self.allClassifyListDetaiArr objectAtIndex:indexPath.row];
    // 给cell中的属性赋值
    cell.nameLabel.text = classifyListDetai.Title;
    // 使用sdwebImage异步加载图片
    NSURL *url = [NSURL URLWithString:classifyListDetai.Cover];
    [cell.FoodImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    
    return cell;
    
}
// 使用AFN进行数据请求
- (void) getClassifyListDetail: (NSInteger)nextPage
{
    NSString *url_Str = [NSString stringWithFormat:@"http://api.hoto.cn/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1405672186599&vc=57&vn=3.7.0&loguid=0&deviceid=haodou864052025053998&uuid=c4affd54-af74-4a3d-8058-d434982df3c3&channel=360_v370&method=Search.getListV3&virtual=limit=20&tagid=%@&order=%ld&offset=0&0&", self.listId, nextPage];
    
    [GetAFN downLoadBlock:url_Str block:^(id result) {
        
        if (self.isUpLoading == NO) {
            // 说明是下拉刷新，就要清空数组中的数据，保持数组中存放的是最新的数据
            [self.allClassifyListDetaiArr removeAllObjects];
        }
        
        NSMutableDictionary *allClassifyDic = result;
        NSMutableDictionary *resultDic = [allClassifyDic objectForKey:@"result"];
        NSMutableArray *classifyArr = [resultDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in classifyArr) {
            ClassifyListDetaiModel *classifyListDetail = [[ClassifyListDetaiModel alloc] init];
            [classifyListDetail setValuesForKeysWithDictionary:dic];
            [self.allClassifyListDetaiArr addObject:classifyListDetail];
        }
        
        //结束底部刷新效果
        [self.detailCollectionView footerEndRefreshing];
        //结束头部刷新效果
        [self.detailCollectionView headerEndRefreshing];
        // 刷新CollectionView
        [self.detailCollectionView reloadData];
        
        // 加载完成,取消菊花
        [self.HUD removeFromSuperview];
        
    }];
    
    
}
// 下拉刷新
- (void)addHeader
{
    __block ClassifyDetailViewController *classifyDetailVC = self;
    
    // 添加下拉刷新头部控件
    [self.detailCollectionView addHeaderWithCallback:^{
        
        // 标记为下拉操作
        classifyDetailVC.isUpLoading = NO;
        // 重新请求数据
        [classifyDetailVC getClassifyListDetail:0];
        
    }];
    
    
// 自动刷新(一进入程序就下拉刷新)
    [self.detailCollectionView headerBeginRefreshing];
    
    
}
// 上拉加载更多
- (void)addFooter
{
    __block ClassifyDetailViewController *classifyDetailVC = self;
    // 添加上拉刷新尾部控件
    [self.detailCollectionView addFooterWithCallback:^{
        
        // 标记为上拉操作
        classifyDetailVC.isUpLoading = YES;
        // 每刷新一级, nextpage加1
        classifyDetailVC.nextPage = classifyDetailVC.nextPage + 1;
        
        // 判断刷新的页面, 最多为4张(以为后面没有数据了)
        if (classifyDetailVC.nextPage >= 4) {
            //结束底部刷新效果
            [classifyDetailVC.detailCollectionView footerEndRefreshing];
            return;
        }
        // 请求更多数据
        [classifyDetailVC getClassifyListDetail:classifyDetailVC.nextPage];
    }];
    
    
}
// 点击item跳转
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    ClassifyListDetaiModel *classifyListDetai = [self.allClassifyListDetaiArr objectAtIndex:indexPath.row];
    // 将title的值传到下一页面
    detailVC.titleStr = classifyListDetai.Title;
    // 将RecipeId的值传到下一页面
    detailVC.rid = classifyListDetai.RecipeId;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
